<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.js"></script>
</head>
<body>
    <div id="app">
        <grand-father></grand-father>
    </div>
</body>
<script>
Vue.component('grand-father',{
    template:`<div>
         爷爷
         <father></father>   
        </div>
    `,
    provide(){
        return{
            grandFather:this
        }
    },
    data(){
        return{
            money:1000
        }
    },
    methods: {
        pay(value){
            if(this.money-value<=0)
            return
            this.money-=value
        }
    },
})
Vue.component('father',{
    props:['money'],
    template:`<div>
         爸爸
         <son></son>   
        </div>
    `
})
Vue.component('son',{
    inject:['grandFather'],
    template:`
    <div>孙子--{{grandFather.money}}
        <button @click="grandFather.pay(100)">花钱的</button>
        </div>
    `
})
new Vue({
        data() {
            return {
                money: 1000
            }
        },
        el: '#app'
    })

</script>
</html>